//
//  LPHomeMenuSiJuJiMustListViewController.m
//  ZhongWangYuanApp
//
//  Created by liuyingping on 2018/6/6.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LPHomeMenuSiJuJiMustListViewController.h"
#import "LPZWYChooseClassClassModel.h"
#import "LPZWYCommonSpecilTableViewCell.h"
#import "YBProjectDetailViewController.h"
#define spicalCell @"LPZWYCommonSpecilTableViewCell"
@interface LPHomeMenuSiJuJiMustListViewController ()<UITableViewDelegate,UITableViewDataSource>
/**    */
@property (strong, nonatomic) UITableView  *tableView;
@property (nonatomic , strong) NSMutableArray * dataArray;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/**    */
@property (strong, nonatomic) NSString  *type;



@end

@implementation LPHomeMenuSiJuJiMustListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"必修专题";
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 0;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self layoutUI];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    [LPRequestManager request_GetPlanInClassListWithpageindex:self.page * 10 pagesize:10   successBlock:^(id object) {
        NSString *result = [object objectForKey:@"code"];
        if ([result integerValue] == 1) {
            NSArray *array = [object objectForKey:@"data"];
            if (array.count == 0) {
                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                
            }else{
                weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array] mutableCopy];
                [weakSelf.tableView reloadData];
            }
            
        }else{
            [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
        }
        [weakSelf.tableView.mj_header endRefreshing];
        
    } failBlock:^(id object) {
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
}

//上拉加载
- (void)netWorking_footer{
    
    __weak typeof(self) weakSelf = self;
     [LPRequestManager request_GetPlanInClassListWithpageindex:self.page * 10 pagesize:10   successBlock:^(id object) {
        NSString *result = [object objectForKey:@"code"];
        if ([result integerValue] == 1) {
            NSArray *array = [object objectForKey:@"data"];
            if (array.count == 0) {
                _page --;
            }else{
                [weakSelf.dataArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array]] ;
                [weakSelf.tableView reloadData];
            }
            
        }
        
        [weakSelf.tableView.mj_footer endRefreshing];
    } failBlock:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.navigView , 0)
    .bottomSpaceToView(self.view , 0);
    
    
}


#pragma mark - 私有方法


#pragma mark - 监听事件



#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 125 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LPZWYCommonSpecilTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:spicalCell forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    cell.isShowEnd = 0;
    return cell;
    
    
}

/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  
    [self clickApply:self.dataArray[indexPath.row] section:indexPath.section index:indexPath.row];
  
    
}

#pragma mark - 报名
- (void)clickApply:(LPZWYChooseClassClassModel *)model section:(NSInteger)section index:(NSInteger)index{
    __weak typeof(self) weakSelf = self;
   
    [LPCustomClassObjTools classApplyWithClassid:model Success:^(id object) {
        model.appmainstatus = @"0";
        self.dataArray[index] = model;
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:index inSection:section];
        [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    }];

    
}


#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_NavViewHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_NavViewHeight ) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
        [_tableView registerClass:[LPZWYCommonSpecilTableViewCell class] forCellReuseIdentifier:spicalCell];
        [_tableView adaptiveIOS11];
        
    }
    return _tableView;
}


- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

@end
